//
//  BGWelcomeViewController.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/23.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

class BGWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupUI()
        self.setupLayout()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.setAnim()
    }
    
    //MARK: - 设置动画
    private func setAnim() {
        //修改约束
        avaterImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animateWithDuration(1.5, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: [], animations: {
            //刷新界面
            self.view.layoutIfNeeded()
            
            }) { (_) in
                UIView.animateWithDuration(0.5, animations: { 
                    //浮现字体
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) in
                        //发送通知切换根控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "welcome")
                })
                
        }
    }
    
    //MARK: - 设置视图
    private func setupUI() {
        view.addSubview(avaterImageView)
        view.addSubview(welcomeLabel)
    }
    
    private func setupLayout() {
        avaterImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
            make.size.equalTo(CGSize.init(width: 90, height: 90))
        }
        
        welcomeLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(avaterImageView.snp_bottom).offset(20)
        }
        
    }
    
    //MARK: - 懒加载
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(imageName: "ad_background")
    //头像
    private lazy var avaterImageView: UIImageView = {
        let avater = UIImageView(imageName: "avatar_default_big")
        
        //从网络下载头像
        avater.bg_setImageWithURL(BGUserAccountViewModel.sharedTools.userAccount?.avatar_large, placeholderImageName: "avatar_default_big")
        //切圆
        avater.layer.cornerRadius = 45
        avater.layer.masksToBounds = true
        //边框
        avater.layer.borderWidth = 2
        avater.layer.borderColor = ThemeColor.CGColor
        
        return avater
    }()
    //欢迎字条
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel(textColor: ThemeColor, textFontSize: 20)
        
        label.text = "欢迎回来"
        label.alpha = 0
        return label
    }()
    
    
}
